package dao;

import dao.imp.ITrainDao;
import entity.Job;
import entity.Train;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import util.MysqlConnUtil;

import java.util.List;

/**
 * @program: personnel_management_system
 * @description: train dao
 * @author: Mr.Sun
 * @create: 2020-05-25 15:00
 **/

public class TrainDao implements ITrainDao {
    private JdbcTemplate template = new JdbcTemplate(MysqlConnUtil.getDataSource());

    public TrainDao() throws Exception {
    }

    @Override
    public boolean insert(Train train) {
        String sql ="insert into train(date,duration,emp_id) values(?,?,?)";
        Integer count = template.update(sql,train.getDate(),train.getDuration(),train.getEmp_id());
        return count == 1;
    }

    @Override
    public boolean update(Train train) {
        boolean res = false;
        String sql = "UPDATE train SET date= ?,duration =?,emp_id =? WHERE tr_id = ?";
        int count = template.update(sql,train.getDate(),train.getDuration(),train.getEmp_id(),train.getTr_id());
        res = count == 1;
        return res;
    }

    @Override
    public boolean delete(Train train) {
        boolean res =false;
        String sql = "DELETE FROM train WHERE tr_id = ?";
        int count = template.update(sql,train.getTr_id());
        return count == 1;
    }

    @Override
    public List<Train> selectAll() {
        String sql = "select * from train";
        List<Train> trains = template.query(sql, new BeanPropertyRowMapper<Train>(Train.class));
        return trains;
    }

    @Override
    public List<Train> select(Train train) {
        return null;
    }
}
